package com.yeyks.controller.member;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.ImmutableMap;
import com.yeyks.common.ajax.AjaxResponseBody;
import com.yeyks.common.base.Arguments;
import com.yeyks.common.base.PageResult;
import com.yeyks.common.param.member.MemberCardOperatorListParam;
import com.yeyks.common.result.member.MemberCardOperatorListResult;
import com.yeyks.commonReference.service.account.AccountInfoService;
import com.yeyks.commonReference.service.consumer.MemberCardService;
import com.yeyks.consumer.dal.domain.MemberCard;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 运营商
 *
 * @author shuai
 */
@Api(tags = "运营商")
@RestController
@RequestMapping("/operator")
public class MemberCardOperatorController {
    @Autowired
    private AccountInfoService accountInfoService;
    @Autowired
    private MemberCardService memberCardService;

    @ApiOperation(value = "v4.3 --> 列表", response = MemberCardOperatorListResult.class)
    @PostMapping(value = "/list")
    public AjaxResponseBody<PageResult<MemberCardOperatorListResult>> selectOperatorList(MemberCardOperatorListParam param) {
        PageResult<MemberCardOperatorListResult> result = accountInfoService.selectOperatorList(param);
        return AjaxResponseBody.success(result);
    }

    @ApiOperation(value = "v4.3 --> 查看销售码")
    @ApiImplicitParam(paramType = "query", value = "运营商id", name = "id", required = true, type = "int")
    @PostMapping(value = "/getCode")
    public AjaxResponseBody<Map<String, Object>> getCode(@RequestParam(name = "id") Integer id) {
        MemberCard one = memberCardService.getOne(new QueryWrapper<MemberCard>()
                .eq(MemberCard.CARD_TYPE, 2)
                .eq(MemberCard.ENABLE, 0)
                .eq(MemberCard.STATUS, 1)
                .eq(MemberCard.SALE_TYPE, 2)
                .eq(MemberCard.SALER_ID, id)
                .orderByAsc(MemberCard.CREATE_TIME)
                .last("limit 1")
        );
        Arguments.beanIsNull(one, "您没有可使用的卡");
        return AjaxResponseBody.success(ImmutableMap.of("cardNo", one.getCardNo()));
    }

}
